package com.henry.producer;

import com.henry.bean.MsgDTO;
import com.henry.enums.ExchangeName;
import com.henry.enums.RoutingKeyName;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class MsgProducer {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 订阅模式发送消息
     */
    public void directSendMsg(MsgDTO msgDTO) {
        this.rabbitTemplate.convertAndSend(ExchangeName.DIRECT_EXCHANGE.name(),
                RoutingKeyName.DIRECT_MSG_ROUTING_KEY.name(),
                msgDTO);
    }

    /**
     * topic模式发送消息
     */
    public void topicSendMsg(MsgDTO msgDTO, String routingKey) {
        this.rabbitTemplate.convertAndSend(ExchangeName.TOPIC_EXCHANGE.name(),
                routingKey,
                msgDTO);
    }

}
